package com.game.centerserver.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.centerserver.common.PageFactory;
import com.game.centerserver.infrastructure.dao.OrderDao;
import com.game.centerserver.infrastructure.entity.Order;
import com.game.centerserver.infrastructure.service.OrderService;
import com.game.centerserver.pojo.request.OrderRequest;
import com.game.centerserver.service.OrderServiceService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: tian
 * @Date: 2024/11/19/16:00
 * @Description:
 */
@Service
public class OrderServiceServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderServiceService {

    @Resource
    private OrderService orderService;

    /**
     * 通过ID查询单条数据
     *
     * @param orderNum 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(String orderNum) {
        return this.orderService.queryById(orderNum);
    }

    /**
     * 根据任意字段查询order表
     * @param order
     * @return
     */
    @Override
    public List<Order> query(Order order) {
        return orderService.query(order);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order insert(Order order) {
        return this.orderService.insert(order);
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Order order) {
        return this.orderService.update(order);
    }

    /**
     * 通过主键删除数据
     *
     * @param orderNum 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String orderNum) {
        return this.orderService.deleteById(orderNum);
    }

    @Override
    public Page<Order> page(OrderRequest orderRequest) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper();
        wrapper.eq(ObjectUtil.isNotEmpty(orderRequest.getUid()), Order::getUid, orderRequest.getUid());
        wrapper.eq(ObjectUtil.isNotEmpty(orderRequest.getProductId()), Order::getProductId, orderRequest.getProductId());
        if (ObjectUtil.isNotEmpty(orderRequest.getDayTime())) {
            DateTime beginOfDay = DateUtil.beginOfDay(sdf.parse(orderRequest.getDayTime()));
            DateTime endOfDay = DateUtil.endOfDay(sdf.parse(orderRequest.getDayTime()));
            wrapper.le(Order::getCreateTime, beginOfDay);
            wrapper.ge(Order::getCreateTime, endOfDay);
        }
        if (!ObjectUtil.isAllEmpty(orderRequest.getStartTime(), orderRequest.getEndTime())) {
            wrapper.le(Order::getCreateTime, DateUtil.beginOfDay(sdf.parse(orderRequest.getStartTime())));
            wrapper.ge(Order::getCreateTime, DateUtil.endOfDay(sdf.parse(orderRequest.getEndTime())));
        }
        Page<Order> page = this.page(PageFactory.defaultPage(), wrapper);
        return page;
    }


}
